import AJAX from '@/common/ajax'

export const GET_WEEK = 'GET_WEEK'
export const GET_LIST = 'GET_LIST'
export const GET_MONTH = 'GET_MONTH'
export const GET_LIKE_VOD = 'GET_LIKE_VOD'

function isEmpty(val, type) {
  return val === undefined || val === '' || val === '-' ? (type ? 'addtime' : '') : val
}

function stateKey(params) {
  const { id, mcid, year, area, wd, letter, lz, order, limit = 10, p = 0 } = params
  return (
    (id || 3) + isEmpty(mcid) + isEmpty(year) + isEmpty(area) + isEmpty(wd) + isEmpty(letter) + isEmpty(lz) + isEmpty(order, 1) + limit + p
  )
}

export default {
  namespaced: true,
  state: {
    Week: '',
    list: [],
    month: [],
    likevod: []
  },
  mutations: {
    [GET_WEEK](state, data) {
      state.playWeek = data
    },
    [GET_LIST](state, { data, params }) {
      state.list[stateKey(params)] = data
    },
    [GET_MONTH](state, { data, params }) {
      state.month[params.month] = data
    },
    [GET_LIKE_VOD](state, { data, params }) {
      state.likevod[`like-${params.not}`] = data
    }
  },
  actions: {
    async getWeek({ commit, state }) {
      const data = state.playWeek
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'list',
          data: {
            react: 1,
            limit: 1000,
            p: 0
          }
        })
        commit(GET_WEEK, data)
        return data
      }
    },
    async getList({ commit, state }, params) {
      const data = state.list[stateKey(params)]
      const { id = 3, mcid, year, area, wd = '', letter, lz, order, limit = 10, p = 0, not } = params
      const reMcid = isEmpty(mcid)
      const reYear = isEmpty(year)
      const reArea = isEmpty(area)
      const reLetter = isEmpty(letter)
      const reLz = isEmpty(lz)
      const reOrder = isEmpty(order, 1)
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'list',
          data: {
            id,
            wd,
            limit,
            not,
            p,
            mcid: reMcid,
            year: reYear,
            area: reArea,
            reLetter: reLetter,
            lz: reLz,
            order: reOrder
          }
        })
        commit(GET_LIST, {
          data,
          params
        })
        return data
      }
    },
    async getMonth({ commit, state }, params) {
      const data = state.month[params.month]
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'month',
          data: {
            month: params.month
          }
        })
        commit(GET_MONTH, {
          data,
          params
        })
        return data
      }
    },
    async getLikeVod({ commit, state }, params) {
      const data = state.likevod[`like-${params.not}`]
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'list',
          data: {
            actor: params.actor,
            not: params.not,
            limit: params.limit,
            p: 0
          }
        })
        commit(GET_LIKE_VOD, {
          data,
          params
        })
        return data
      }
    }
  }
}
